import React, {Component} from 'react';
import {Menu, Icon, Button} from 'antd';
import fetch from 'isomorphic-fetch';
import {Link, BrowserRouter, Route} from 'react-router-dom';
import GameInfo from '../gameInfo/GameInfo'
import UserInfo from '../userInfo/UserInfo'
import TeamInfo from '../teamInfo/TeamInfo'
import {BaseUrl} from "../../common/BaseUrl";
import * as base from "../../common/BaseUrl";


class MainMenu extends Component {
    constructor(props) {
        super(props);
        console.log(props.history);
        this.handleClick = this.handleClick.bind(this);
        this.loginOut = this.loginOut.bind(this);
    }

    handleClick(item, key, keyPath) {

    }

    componentWillMount() {
        this.props.history.push("/main/userInfo", null);
    }

    loginOut() {
        let url = BaseUrl + "logout";
        fetch(url, {
            method: 'post',
            header: {
                'Accept': 'application/json',
                'Access-Control-Allow-Origin': '*',
            },
            credentials: 'include',
            redirect: 'follow',
            mode: 'cors'
        }).then(res => {
            if (res.ok) {
                this.props.history.push("/login", null);
            }
        }).catch(err => {
            console.log(err);
        })
    }


    /*componentDidMount() {
        this.props.history.push("/main/userInfo",null);
    }*/

    render() {
        const SubMenu = Menu.SubMenu;
        const MenuItemGroup = Menu.ItemGroup;
        return (
            <div style={{height: 949}}>
                <div style={{height: '5%', backgroundColor: "#fafafa"}}>
                    <Button style={{marginRight: '20px'}} onClick={this.loginOut}>登出</Button>
                </div>
                <div style={{height: '95%'}}>
                    <div style={{width: '13%', height: '100%', float: 'left'}}>
                        <Menu
                            onClick={this.handleClick}
                            style={{width: 240}}
                            defaultSelectedKeys={['1']}
                            defaultOpenKeys={['sub1']}
                            mode="inline">
                            <SubMenu key="sub1" title={<span><Icon type="mail"/><span>基础信息</span></span>}>
                                <Menu.Item key="1">
                                    <Link to='/main/userInfo'>用户信息</Link>
                                </Menu.Item>
                                <Menu.Item key="2">
                                    <Link to='/main/gameInfo'>游戏信息</Link>
                                </Menu.Item>
                                <Menu.Item key="3">
                                    <Link to='/main/teamInfo'>团队信息</Link>
                                </Menu.Item>
                            </SubMenu>
                        </Menu>
                    </div>
                    <div style={{width: '87%', height: '100%', float: 'left', padding: '20px'}}>
                        <Route path="/main/gameInfo" component={GameInfo}/>
                        <Route path="/main/userInfo" component={UserInfo}/>
                        <Route path="/main/teamInfo" component={TeamInfo}/>
                    </div>
                </div>
            </div>
        );
    }
}

export default MainMenu;